package com.zheng.api.server.service.base.pay;

import com.zheng.common.constant.PayConstant;
import com.zheng.common.util.DateUtil;
import com.zheng.pay.dao.model.PayPayOrder;
import com.zheng.pay.dao.model.PayPayOrderExample;
import com.zheng.pay.rpc.api.PayPayOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: lvb
 * @date: 17/10/30
 * @description:
 */
@Service
public class Base4PayOrderService extends BaseCommonService {

    @Autowired
    private PayPayOrderService payPayOrderService;

    public int baseCreatePayOrder(PayPayOrder payOrder) {
        return payPayOrderService.insertSelective(payOrder);
    }

    public PayPayOrder baseSelectPayOrder(String payOrderId) {
        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPayorderidEqualTo(payOrderId);
        return payPayOrderService.selectFirstByExample(example);
    }

    public PayPayOrder baseSelectByMchIdAndPayOrderId(String mchId, String payOrderId) {
        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchId);
        criteria.andPayorderidEqualTo(payOrderId);
        List<PayPayOrder> payOrderList = payPayOrderService.selectByExample(example);
        return CollectionUtils.isEmpty(payOrderList) ? null : payOrderList.get(0);
    }

    public PayPayOrder baseSelectByMchIdAndMchOrderNo(String mchId, String mchOrderNo) {
        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchId);
        criteria.andMchordernoEqualTo(mchOrderNo);
        List<PayPayOrder> payOrderList = payPayOrderService.selectByExample(example);
        return CollectionUtils.isEmpty(payOrderList) ? null : payOrderList.get(0);
    }

    public int baseUpdateStatus4Ing(String payOrderId, String channelOrderNo) {
        PayPayOrder payOrder = new PayPayOrder();
        payOrder.setStatus(PayConstant.PAY_STATUS_PAYING);
        if(channelOrderNo != null) payOrder.setChannelorderno(channelOrderNo);
        //payOrder.setPaysucctime(System.currentTimeMillis());
        payOrder.setUpdatetime(DateUtil.getDate());
        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPayorderidEqualTo(payOrderId);
        criteria.andStatusEqualTo(PayConstant.PAY_STATUS_INIT);
        return payPayOrderService.updateByExampleSelective(payOrder, example);
    }

    public int baseUpdateStatus4Success(String payOrderId) {
        return baseUpdateStatus4Success(payOrderId, null);
    }

    public int baseUpdateStatus4Success(String payOrderId, String channelOrderNo) {
        PayPayOrder payOrder = new PayPayOrder();
        //payOrder.setPayorderid(payOrderId);
        payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
        payOrder.setPaysucctime(DateUtil.getDate());
        payOrder.setUpdatetime(DateUtil.getDate());
        if(StringUtils.isNotBlank(channelOrderNo)) payOrder.setChannelorderno(channelOrderNo);
        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPayorderidEqualTo(payOrderId);
        criteria.andStatusEqualTo(PayConstant.PAY_STATUS_PAYING);
        return payPayOrderService.updateByExampleSelective(payOrder, example);
    }

    public int baseUpdateStatus4Complete(String payOrderId) {
        PayPayOrder payOrder = new PayPayOrder();
        //payOrder.setPayorderid(payOrderId);
        payOrder.setStatus(PayConstant.PAY_STATUS_COMPLETE);
        payOrder.setLastnotifytime(DateUtil.getDate());
        PayPayOrderExample example = new PayPayOrderExample();
        PayPayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPayorderidEqualTo(payOrderId);
        criteria.andStatusEqualTo(PayConstant.PAY_STATUS_SUCCESS);
        return payPayOrderService.updateByExampleSelective(payOrder, example);
    }

    public int baseUpdateNotify(String payOrderId, byte count) {
        PayPayOrder newPayOrder = new PayPayOrder();
        newPayOrder.setNotifycount(count);
        newPayOrder.setLastnotifytime(DateUtil.getDate());
        newPayOrder.setPayorderid(payOrderId);
        return payPayOrderService.updateByPrimaryKeySelective(newPayOrder);
    }

}
